//
//  IPWebViewPlayer.swift
//  iPlayer
//
//  Created by jackcheng on 2017/11/17.
//  Copyright © 2017年 xiangyue. All rights reserved.
//

import UIKit
import WebKit
class IPWebViewPlayer: IPViewController,WKUIDelegate,WKNavigationDelegate,UIGestureRecognizerDelegate {

	lazy var webView:WKWebView = {
		let config = WKWebViewConfiguration.init()
		config.allowsInlineMediaPlayback = true
		let webView:WKWebView = WKWebView.init(frame: CGRect.init(x: 0, y: kNavBarH, width: kScreenW, height: kScreenH - kNavBarH), configuration: config)
		webView.uiDelegate = self
		webView.navigationDelegate = self
		self.navigationItem.rightBarButtonItem = UIBarButtonItem.init(title: "播放", style: UIBarButtonItemStyle.plain, target: self, action: #selector(play))
		
		let tap = UITapGestureRecognizer.init(target: self, action: #selector(tap(tgr:)))
		webView.addGestureRecognizer(tap)
		tap.delegate = self
		tap.cancelsTouchesInView = false
		return webView
	}()
	
	
	@objc func tap(tgr:UITapGestureRecognizer) {
//		print(urlS)
//		let js = "(document.getElementsByTagName(\"video\")[0]).src"
//		webView.evaluateJavaScript(js) { (res, error) in
//			if res == nil {
//				return
//			}
//			let sre = res as! String
//			if sre.lengthOfBytes(using: .utf8) == 0 {
//				return
//			}
//			print("res--" + sre)
//			let mvc = IPlayerController.init(urls: [sre])
//			self.present(mvc, animated: true, completion: nil)
//		}
	}
	
	@objc func play() {
		//		print(urlS)
		let js = "(document.getElementsByTagName(\"video\")[0]).src"
		webView.evaluateJavaScript(js) { (res, error) in
			if res == nil {
				return
			}
			let sre = res as! String
			if sre.lengthOfBytes(using: .utf8) == 0 {
				return
			}
			print("res--" + sre)
			let mvc = IPlayerController.init(urls: [sre])
			self.present(mvc, animated: true, completion: nil)
		}
	}
	
	
	var urlString:String!
	
	init(urlString:String) {
		super.init(nibName: nil, bundle: nil)
		self.urlString = urlString
	}
	
	required init?(coder aDecoder: NSCoder) {
		fatalError("init(coder:) has not been implemented")
	}
	
	override func viewDidLoad() {
		super.viewDidLoad()
		self.view.addSubview(self.webView)
		self.webView.load(URLRequest.init(url: URL.init(string: self.urlString)!))
		
	}
	// 页面开始加载时调用
	func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
		
	}
	
	func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
		
	}
	
	func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
		let js = "(document.getElementsByTagName(\"video\")[0]).src"
		webView.evaluateJavaScript(js) { (res, error) in
			if res == nil {
				self.navigationItem.rightBarButtonItem?.isEnabled = false
				return
			}
			let sre = res as! String
			if sre.lengthOfBytes(using: .utf8) == 0 {
				self.navigationItem.rightBarButtonItem?.isEnabled = false
				return
			}
			print("res--" + sre)
			self.navigationItem.rightBarButtonItem?.isEnabled = true
		}
	}
	
	func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
		
	}
	
	// 接收到服务器跳转请求之后调用
	
	func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {

	}

	func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
		self.navigationItem.rightBarButtonItem?.isEnabled = false
		decisionHandler(.allow)
	}

	func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
		decisionHandler(.allow)
	}
	
	/**
	*  web界面中有弹出警告框时调用
	*
	*  @param webView           实现该代理的webview
	*  @param message           警告框中的内容
	*  @param frame             主窗口
	*  @param completionHandler 警告框消失调用
	*/
	
	func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
		print("message---" + message)
		print(frame)
	}
	
	func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
		return true
	}
	
	override func didReceiveMemoryWarning() {
		super.didReceiveMemoryWarning()
		// Dispose of any resources that can be recreated.
	}
	
}
